require('../css/common/normalize.css')
require('../css/common/reset.css')
require('../css/common/property.css')
require('../css/common/basic.less')
require('../lib/fonts/iconfont.css')
require('../css/me-information.less')

//导入axios
const axios = require("axios");

//导入weui
require("weui")
const weui = require("weui.js")


// 当我们点击回退的时候，页面回退到上一个页面
document.addEventListener('DOMContentLoaded', function () {
    //我们要进行数据回调

    axios.get('http://139.9.177.51:8099/users/accountinfo', {
        params: {
            userId: localStorage.getItem('userId')
        }
    }
    ).then(function (res) {
        console.log(res.data.data);
        let reBack = res.data.data

        if (reBack.imgurl) {
            document.querySelector('#imgUrl').src = reBack.imgurl
        }
        if (reBack.nickname) {
            document.querySelector('#js_input2').value = reBack.nickname
        }
        if (reBack.gender) {
            document.querySelector('#sex1').textContent = reBack.gender
        }
        if (reBack.birthday) {
            document.querySelector('#birthday').textContent = reBack.birthday.substr(0, 10)
        }
        if (reBack.address) {
            document.querySelector('#prince').textContent = reBack.address.split(',')[0]
            document.querySelector('#city').textContent = reBack.address.split(',')[1]
        }
        if (reBack.sign) {
            document.querySelector('#textA').textContent = reBack.sign
            document.querySelector('#num').textContent = reBack.sign.length
        }
    })

    //性别

    document.querySelector('#sexPicker').addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                console.log(result[0].label);
                document.querySelector('#sex1').textContent = result[0].label

                aa = result[0].label

            },
            title: '请选择性别'
        });
    });


    document.querySelector('#meMeans').addEventListener('click', function () {
        history.go(-1)
    })

    // 所在省

    // document.querySelector('#princePicker').addEventListener('click', function () {
    //     weui.picker([{
    //         label: '四川省',
    //         value: 0
    //     }, {
    //         label: '辽宁省',
    //         value: 1
    //     }, {
    //         label: '山东省',
    //         value: 2
    //     }, {
    //         label: '云南省',
    //         value: 3
    //     }], {
    //         onConfirm: function (result) {
    //             console.log(result[0].label);
    //             document.querySelector('#prince').textContent = result[0].label

    //             cc = result[0].label
    //         }
    //     })
    // })



    // 所在省

    let proArr = []
    let cityArr = []
    //我们要获取省的信息从后台里面
    axios.get("http://139.9.177.51:8099/address/province").then(function (res) {

        //{addressId: 110000, name: '北京市', pid: 0}
        console.log(res.data.data);
        let pro = res.data.data
        proArr = pro.map(function (item) {
            return { label: item.name, value: item.addressId }
        })

        console.log(proArr);

    })




    document.querySelector('#princePicker').addEventListener('click', function () {
        weui.picker(proArr, {
            onConfirm: function (result) {
                console.log(result[0].label);
                document.querySelector('#prince').textContent = result[0].label
                cc = result[0].label
                let pid = result[0].value
                console.log(pid);

                axios.get('http://139.9.177.51:8099//address/city/' + pid).then(function (res) {
                    let city2 = res.data.data;
                    cityArr = city2.map(function (items) {
                        return { label: items.name, value: items.addressId }

                    })
                    document.querySelector('#city').textContent = '选择城市'
                    console.log(cityArr);
                })

            },
            title: '请选择省份'
        });
    });


    //所在市



    document.querySelector('#cityPicker').addEventListener('click', function () {
        weui.picker(cityArr, {

            onConfirm: function (result) {
                console.log(result[0].label);
                document.querySelector('#city').textContent = result[0].label
                ee = result[0].label
            },
            title: '请选择所在城市'
        });
    });

    // 生日  多列选择器

    function buZero(a) {
        return a < 10 ? "0" + a : a
    }
    document.querySelector('#birthdayPicker').addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                console.log(result);

                let data = [result[0].value, buZero(result[1].value), buZero(result[2].value)].join('-')
                console.log(data);
                document.querySelector('#birthday').textContent = data

            },
            title: '请选择出生日期'
        });
    });


    // 更换头像
    /*
    头像上传:
      1.获取是input[type="file"]的files 
      2.参数需要使用FormData对象  let fd=new FormData()  fd.append("xxx",file)
      3.上传的参数是fd对象
    */

    document.querySelector('#headPortrait').addEventListener('change', function () {

        console.log(1234);

        //console.log(this.files[0]);
        let file = this.files[0]; //获取上传的文件
        console.log(file);
        // 实例化一个formData对象
        let fd = new FormData()

        //将参数追加到formData里面
        fd.append('imgurl', file)

        axios.post('http://139.9.177.51:8099/users/upload', fd).then(function (res) {
            console.log(res);
            console.log(res.data.status);
            if (res.data.status == 0) {
                weui.toast('图片上传成功')
                document.querySelector('#imgUrl').src = 'http://139.9.177.51:8099' + res.data.data
                dd = 'http://139.9.177.51:8099' + res.data.data



            } else {
                weui.toast('图片上传失败，图片格式不对')
            }

        })

    })
    //优化图片上传

    document.querySelector('#imgUrl').addEventListener('click', function () {
        document.querySelector('#headPortrait').click()
    })

    //保存数据存入ajax
    document.querySelector('#saveBtn2').addEventListener('click', function () {

        console.log(1250352);


        axios.post('http://139.9.177.51:8099/users/userEdit', {
            imgurl: document.querySelector('#imgUrl').src,
            nickname: document.querySelector('#js_input2').value,
            gender: document.querySelector('#sex1').textContent,
            birthday: document.querySelector('#birthday').textContent,
            address: [document.querySelector('#prince').textContent, document.querySelector('#city').textContent],
            sign: document.querySelector('#textA').value,
            userId: localStorage.getItem('userId')
        }).then(function (res) {
            console.log(res.data.status);
            if (res.data.status === 0) {
                weui.toast('修改成功')
                location.href = './me.html'
            } else {
                weui.toast('修改失败')
            }


        })
    })

    //将个性签名的数字动起来

    document.querySelector('#textA').addEventListener('input', function () {
        console.log(1253);
        console.log(this.value.length);
        document.querySelector('#num').textContent = this.value.length

        if (this.value.length > 50) {
            weui.toast('亲，字数超过50了哦')
        }
    })


})




